package com.github.ginirohikocha.dbm.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.ginirohikocha.dbm.R;
import com.github.ginirohikocha.dbm.activity.LoginActivity;
import com.github.ginirohikocha.dbm.util.XingeUtil;

public class MeFragment extends Fragment {

    private Activity activity;

    private TextView name, phone, company, workerNum;
    private Button logout;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View me = inflater.inflate(R.layout.fragment_me_act, container, false);
        name = me.findViewById(R.id.fragment_me_name);
        phone = me.findViewById(R.id.fragment_me_mobilephone);
        company = me.findViewById(R.id.fragment_me_company);
        logout = me.findViewById(R.id.fragment_me_logout);
        workerNum = me.findViewById(R.id.fragment_me_workerNum);

        final SharedPreferences workerInfo = activity.getSharedPreferences("worker", 0);
        name.setText(workerInfo.getString("workerName", "暂无"));
        phone.setText(workerInfo.getString("mobilephone", "暂无"));
        company.setText(workerInfo.getString("companyName", "暂无"));
        workerNum.setText(workerInfo.getString("workerNum", "暂无"));
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XingeUtil.logout(activity.getApplicationContext(), workerInfo.getString("mobilephone", ""));
                startActivity(new Intent(activity, LoginActivity.class));
                activity.finish();
            }
        });
        return me;
    }

    public static MeFragment newInstance() {
        
        Bundle args = new Bundle();
        
        MeFragment fragment = new MeFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
